﻿using System;
using System.Configuration;
using System.Linq;
using System.Xml;

namespace Dotnet.Utils.Utility.Config
{
    public static class SysConfigUtil
    {
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            bool flag = false;
            foreach (object obj in ConfigurationManager.AppSettings)
            {
                string a = (string)obj;
                if (a == newKey)
                {
                    flag = true;
                }
            }
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (flag)
            {
                configuration.AppSettings.Settings.Remove(newKey);
            }
            configuration.AppSettings.Settings.Add(newKey, newValue);
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static string GetAppConfig(string strKey)
        {
            string result;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(strKey))
            {
                result = ConfigurationManager.AppSettings[strKey];
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static string GetConnectionStringsConfig(string connectionName)
        {
            string text = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
            Console.WriteLine(text);
            return text;
        }

        public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)
        {
            bool flag = false;
            if (ConfigurationManager.ConnectionStrings[newName] != null)
            {
                flag = true;
            }
            ConnectionStringSettings settings = new ConnectionStringSettings(newName, newConString, newProviderName);
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (flag)
            {
                configuration.ConnectionStrings.ConnectionStrings.Remove(newName);
            }
            configuration.ConnectionStrings.ConnectionStrings.Add(settings);
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

        public static object GetConfigSection(string sConfigSectionName)
        {
            return ConfigurationManager.GetSection(sConfigSectionName);
        }

        public static object Create(object parent, object configContext, XmlNode section)
        {
            Type type = Type.GetType(section.Attributes["type"].Value);
            object[] args = new object[]
            {
                section
            };
            object result = null;
            try
            {
                result = Activator.CreateInstance(type, args);
            }
            catch (Exception)
            {
            }
            return result;
        }
    }
}